﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {
            var sql = "select * from Articles";
            var dt = DbHelper.GetDataTable(sql);

            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        //取消
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            FillData();
        }
        //删除
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GetCellValue(e.RowIndex, 0);
            var sql = string.Format("Delete Articles where Id={0}", id);
            DbHelper.ACD(sql);
            GridView1.EditIndex = -1;
            FillData();
        }

        private string  GetCellValue(int rowIndex, int v)
        {
            var c = GridView1.Rows[rowIndex].Cells[v];
            var str = c.Controls.Count > 0 ? ((TextBox)c.Controls[0]).Text : c.Text;
            return str;
        }

        //进入编辑
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            FillData();
        }
        //更新
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var idString = GetCellValue(e.RowIndex, 0);
            var inId = int.TryParse(idString, out int id);
            var Title = GetCellValue(e.RowIndex, 1);
            var Content = GetCellValue(e.RowIndex, 2);
            var Author = GetCellValue(e.RowIndex, 3);
            var Remarks = GetCellValue(e.RowIndex, 4);
            if (inId)
            {
                var sql = string.Format("update Articles set Title='{0}',Content='{1}',Author='{2}',Remarks='{3}' where Id={4}", Title, Content, Author, Remarks, id);
                DbHelper.ACD(sql);

            }
            else
            {
                var sql = string.Format("insert into Articles(Title,Content,Author,Remarks)values('{0}','{1}','{2}','{3}')", Title, Content, Author, Remarks);
                DbHelper.ACD(sql);
            }
            GridView1.EditIndex = -1;
            FillData();
        
    }
        //新增
        private static DataTable Table()
        {
            var sql = "select * from Articles";
            var dt = DbHelper.GetDataTable(sql);
            return dt;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            var dt = Table().Copy();
            dt.Rows.Add(dt.NewRow());
            GridView1.EditIndex = dt.Rows.Count - 1;
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
}